<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Role,Auth,App\User,App\permission;
class RoleController extends Controller
{
    public $authRole;
    public $auth;
    public function __construct()
    {
        $this->middleware('auth');

        if($this->auth = Auth::user()){
            $this->authRole = User::find($this->auth->id)->roles[0];
        }

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $roles = Role::where('name','!=','god')->orderBy('id')->paginate(3);
        $authRole = $this->authRole;
        return view('admin.role',compact('roles','authRole'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $authRole = $this->authRole;
        return view('admin.addRole',compact('authRole'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,
            ['name' =>  'required|min:2|max:20|unique:roles,name','display_name' =>  'required']
        );
        $role = new \App\Role();
        $role->name         = $request->name;
        $role->display_name = $request->display_name; // optional
        $role->description  = $request->description; // optional
        $role->save();

        return redirect()->back()->withInfo('创建角色成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $authRole = $this->authRole;
        $role = Role::find($id);
        $perms = Permission::all();
        $rolePerms = Role::find($id)->perms->toArray();
        $roleName = [];
        foreach($rolePerms as $v){
            $roleName[] = $v['name'];
        }
        return view('admin.editRole',compact('authRole','perms','role','roleName'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,
            ['name' =>  'required|min:2|max:20|unique:roles,name,'.$id,'display_name' =>  'required']
        );
        $info = $request->only('name','display_name','description');
        $role = Role::findOrFail($id);
        $role->update($info);
        $role->perms()->sync($request->perms);
        return redirect()->back()->withInfo('修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $role = Role::findOrFail($id); // Pull back a given role

        // Regular Delete
        $role->delete(); // This will work no matter what

        // Force Delete
        $role->users()->sync([]); // Delete relationship data
        $role->perms()->sync([]); // Delete relationship data

        $role->forceDelete(); // Now force delete will work regardless of whether the pivot table has cascading delete
        return redirect()->back()->withInfo('删除成功');
    }
}
